package com.cluster.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cluster.platform.common.api.CommonPage;
import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.common.api.ResultCode;
import com.cluster.platform.dto.CmsContentProfileDto;
import com.cluster.platform.mapper.CmsContentDetailsMapper;
import com.cluster.platform.mapper.CmsContentProfileMapper;
import com.cluster.platform.model.CmsContentProfile;
import com.cluster.platform.service.CmsContentProfileService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName: CmsContentProfileServiceImpl
 */
@Service
public class CmsContentProfileServiceImpl implements CmsContentProfileService {

    @Autowired
    private CmsContentProfileMapper cmsContentProfileMapper;

    @Autowired
    private CmsContentDetailsMapper cmsContentDetailsMapper;

    @Override
    public CommonResult<Object> searchContentProfilePage(CmsContentProfileDto cmsContentProfileDto) {
        if (null == cmsContentProfileDto) {
            return CommonResult.failed("未获取参数信息");
        }
        PageHelper.startPage(cmsContentProfileDto.getPageNum(), cmsContentProfileDto.getPageSize());
        CmsContentProfile cmsContentProfile = BeanUtil.copyProperties(cmsContentProfileDto, CmsContentProfile.class);
        List<CmsContentProfile> cmsContentProfiles = cmsContentProfileMapper.searchContentProfile(cmsContentProfile);
        if (cmsContentProfiles.isEmpty()) {
            CommonResult.failed("为查询到信息");
        }
        return CommonResult.success(CommonPage.restPage(cmsContentProfiles));
    }

    @Override
    @Transactional
    public CommonResult<Object> addContentProfile(CmsContentProfile cmsContentProfile) {
        if (null == cmsContentProfile)
            return CommonResult.failed(ResultCode.PARAM_NULL);
        // 根据文章标题查询是否存在相同标题
        CmsContentProfile searchContentProfileByTitle = cmsContentProfileMapper.searchContentProfileByTitle(cmsContentProfile.getUserId(), cmsContentProfile.getTitle());
        if (Optional.ofNullable(searchContentProfileByTitle).isPresent())
            return CommonResult.failed(ResultCode.REPEAT);
        cmsContentProfile.setStatus(1);
        cmsContentProfile.setInsertDate(new Date());
        int insertSelective = cmsContentProfileMapper.insertSelective(cmsContentProfile);
        if (insertSelective < 0)
            return CommonResult.failed(ResultCode.ADD_FAIL);
        // 查询新增数据完整信息
        CmsContentProfile searchResult = cmsContentProfileMapper.searchContentProfileByTitle(cmsContentProfile.getUserId(), cmsContentProfile.getTitle());
        return CommonResult.success(searchResult);
    }

    @Override
    @Transactional
    public CommonResult<Object> updateContentProfile(CmsContentProfile cmsContentProfile) {
        if (null == cmsContentProfile) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        int update = cmsContentProfileMapper.updateByPrimaryKeySelective(cmsContentProfile);
        if (update < 0) {
            return CommonResult.failed(ResultCode.UPDATE_FAIL);
        }
        return CommonResult.success(cmsContentProfile);
    }

    @Override
    @Transactional
    public CommonResult<Object> delContentProfile(CmsContentProfile cmsContentProfile) {
        if (null == cmsContentProfile) {
            return CommonResult.failed("未获取参数信息");
        }
        cmsContentProfile.setStatus(0);
        int update = cmsContentProfileMapper.updateByPrimaryKeySelective(cmsContentProfile);
        if (update < 0) {
            return CommonResult.failed("更新失败");
        }
        return CommonResult.success(cmsContentProfile);
    }

    @Override
    @Transactional
    public CommonResult<Object> batchContentProfile(Integer status, List<String> ids) {
        if (ids.isEmpty()) {
            return CommonResult.failed("操作失败");
        }
        int updateBatch = cmsContentProfileMapper.batchContentProfile(status, ids);
        if (updateBatch < 0) {
            return CommonResult.failed("更新失败");
        }
        return CommonResult.success(updateBatch);
    }

    @Override
    @Transactional
    public CommonResult<Object> deleteContentProfile(CmsContentProfile cmsContentProfile) {
        if (!Optional.ofNullable(cmsContentProfile).isPresent() || StringUtils.isBlank(cmsContentProfile.getId())) {
            return CommonResult.failed("传入参数为空");
        }
        int deleteStatus = cmsContentProfileMapper.deleteContentProfile(cmsContentProfile.getId());
        int deleteContentDetail = cmsContentDetailsMapper.deleteContentDetail(cmsContentProfile.getId());
        if (deleteStatus > 0 && deleteContentDetail > 0) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }
}
